package com.wencai.util;

import com.wencai.daily.bean.DailyData;
import com.wencai.daily.utils.ReadUtils;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangpan
 * @date 2023/6/9
 */
public class LimitUpCountUtils {
    private static String dir = "D:\\Users\\Administrator\\Desktop\\记录\\所有日线数据\\";

    /**
     * 获取n天内涨停次数
     * 数据的源头是2010-01-01
     *
     * @return
     */
    public static int getCount(String code, String date, int n, double pctChg) throws IOException {
        date = DateConverter.convertDate(date);
        List<String> lines = IOUtils.readFromFile(dir + code + ".txt");
        if(lines.isEmpty()){
            return 0;
        }
        lines.remove(0);
        int index = -1;
        for (int i = 0; i < lines.size(); i++) {
            if (lines.get(i).startsWith(date)) {
                index = i;
            }
        }
        int startIndex = index - n < 0 ? 0 : index - n;
        List<DailyData> dailyData = lines.stream().map(item -> ReadUtils.convert("", item))
                .collect(Collectors.toList());
        List<DailyData> subList = dailyData.subList(startIndex, index).stream().filter(item -> item.getAmount() > 0).collect(Collectors.toList());
        int count = 0;
        for (DailyData data : subList) {
            if (data.getPctChg() > pctChg) {
                count++;
            }
        }
        return count;
    }

    public static int getLimitUpCount(String code, String date, int n) throws IOException {
        return getCount(code, date, n, 9.5);
    }


}
